package engine.systems.collision.collision
{
	import engine.classes.enumerations.SystemPriority;
	import engine.systems.update.GameSystem;
	import engine.utils.CollisionUtils;
	import engine.utils.EntityUtils;
	import engine.utils.GeomUtils;
	
	/**
	 * The Collision System is a very basic collision detection system that checks all collision entities
	 * against each other to see if they are of the same collision type, which defaults to "default" when
	 * no type is specified, causing all collision entities to check all other collision entities. If a
	 * collision between entities occurs, each entity dispatches itself and the colliding entity to
	 * listening handlers.
	 */
	public class CollisionSystem extends GameSystem
	{
		public function CollisionSystem()
		{
			super(CollisionNode, updateNode);
			
			this.priority = SystemPriority.COLLISION;
		}
		
		private function updateNode(node1:CollisionNode, time:Number):void
		{
			for(var node2:CollisionNode = node1.next; node2; node2 = node2.next)
			{
				if(EntityUtils.sleeping(node2.entity)) continue;
				
				if(!node1.collision.types[node2.collision.type]) continue;
				if(!node2.collision.types[node1.collision.type]) continue;
				
				var precise:Boolean = (node1.collision.precise && node2.collision.precise);
				
				if(CollisionUtils.isColliding(node1.render.display, node2.render.display, precise))
				{
					node1.collision.collided.dispatch(node1.entity, node2.entity);
					node2.collision.collided.dispatch(node2.entity, node1.entity);
				}
			}
		}
	}
}